package com.k7.mobile.platform.dataobject;

import java.io.Serializable;
import java.util.Map;
import org.apache.commons.lang.StringUtils;


public class PageDO implements Serializable {

	private static final long serialVersionUID = 3258128059449226041L;


	private String orderStr;
	

	private int toPage = 1;


	private int perPageSize = 10;


	private int totalItem = 0;
	
	private int needPaged = 1;//是否需要分页，1：需要；0：不需要，默认为1


	public int getNeedPaged() {
		return needPaged;
	}

	public void setNeedPaged(int needPaged) {
		this.needPaged = needPaged;
	}

	public boolean isFirstPage() {
		return toPage == 1;
	}

	public String getOrderStr() {
		return orderStr;
	}
	public String getOrderCacheStr() {
		if(orderStr != null)
			return orderStr.trim().replaceAll(" ", "");
		else
			return null;
	}

	public void setOrderStr(String orderStr) {
		this.orderStr = orderStr;
	}

	public int getToPage() {
		if (toPage > getTotalPage()) {
			return getTotalPage();
		} else
			return toPage;
	}

	public void setToPage(int toPage) {
		this.toPage = toPage;
	}

	public int getPerPageSize() {
		return perPageSize;
	}

	public void setPerPageSize(int perPageSize) {
		this.perPageSize = perPageSize;
	}

	public int getTotalItem() {
		return totalItem;
	}

	public void setTotalItem(int totalItem) {
		this.totalItem = totalItem;
	}


	public int getPreviousPage() {
		int back = toPage - 1;
		if (back <= 0) {
			back = 1;
		}
		return back;
	}


	public boolean isLastPage() {
		return this.getTotalPage() == toPage;
	}


	public int getNextPage() {
		int back = toPage + 1;
		if (back > this.getTotalPage()) {
			back = this.getTotalPage();
		}
		return back;
	}


	public int getTotalPage() {
		if (totalItem == 0) {
			return 0;
		}
		int result = totalItem / perPageSize;
		if (totalItem % perPageSize != 0) {
			result++;
		}
		return result;
	}


	public int validateToPage(int toPage) {
		int pageNum = toPage;
		if (pageNum < 1)
			pageNum = 1;
		if (pageNum > getTotalPage())
			pageNum = getTotalPage();
		return pageNum;
	}


	public void buildPageInfo(Map params) {
		String pageSize = (String) params.get("perPageSize");
		String toPage = (String) params.get("toPage");
		String orderStr = (String) params.get("orderStr");
		if (StringUtils.isNotBlank(pageSize)) {
			setPerPageSize(Integer.parseInt(pageSize));
		}
		if (StringUtils.isNotBlank(toPage)) {
			int page = 1;
			try {
				page = Integer.parseInt(toPage);
			} catch (Exception e) {
			}
			setToPage(page);
		}
		if (StringUtils.isNotBlank(orderStr)) {
			setOrderStr(orderStr);
		}
	}


	public void validatePageInfo() {
		setToPage(validateToPage(toPage));
	}


	public Integer getEndRow() {
		if (toPage > 0 && perPageSize > 0) {
			return new Integer(toPage * perPageSize);
		}
		return new Integer(10);
	}

	
	public Integer getStartRow() {
		if (toPage > 0 && perPageSize > 0) {
			return new Integer((toPage - 1) * perPageSize);
		}
		return new Integer(0);
	}

	protected String getSQLBlurValue(String value) {
		if (value == null) {
			return null;
		}
		return value + '%';
	}

	protected String formatDate(String datestring) {
		if (datestring == null || datestring.equals("")) {
			return null;
		} else {
			return (datestring + " 00:00:00");
		}
	}

	protected String addDateEndPostfix(String datestring) {
		if (datestring == null || datestring.equals("")) {
			return null;
		}
		return datestring + " 23:59:59";
	}

	protected String addDateStartPostfix(String datestring) {
		if (datestring == null || datestring.equals("")) {
			return null;
		}
		return datestring + " 00:00:00";
	}
	
	public  int getDesiredPage(){
		return this.toPage;
	}

}